/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minidota.model;

import com.minidota.game.Room;
import com.minidota.utils.Util;
import java.util.ArrayList;

/**
 *
 * @author User
 */
public class Creep extends User {

    private ArrayList<Vector2> pathList = new ArrayList<Vector2>();
    private int line;

    public Creep(Room room, int teamId, int line) {
        super(room);
        setId(getRoom().getFreeGameId());
        setTeamId(teamId);
        setLine(line);
        if (getTeamId() == 2) {
            setHeroId(10);
            if (line == 1) {
                pathList.add(new Vector2(400, 400));
                pathList.add(new Vector2(400, 4100));
            } else if (line == 2) {
                pathList.add(new Vector2(400, 4100));
            } else if (line == 3) {
                pathList.add(new Vector2(4100, 4100));
                pathList.add(new Vector2(400, 4100));
            }
        } else {
            setHeroId(11);
            if (line == 1) {
                pathList.add(new Vector2(400, 400));
                pathList.add(new Vector2(4100, 400));
            } else if (line == 2) {
                pathList.add(new Vector2(4100, 400));
            } else if (line == 3) {
                pathList.add(new Vector2(4100, 4100));
                pathList.add(new Vector2(4100, 400));
            }
        }
        setRadius(getBasicHero().getAtackRadius());
    }

    @Override
    public void updateData(long tick) {
        if (isAlive()) {
            BaseObject t = searchTarget();
            if (t != null) {
                if (getTarget() == null || (getTarget() != null && t.getGameId() != getTarget().getGameId())) {
                    setTarget(t);
                    setCommand(2);
                }
            } else {
                if (getPath().size() == 0) {
                    if (getPathList().size() > 0) {
                        if (getPosition().sub(getPathList().get(0)).lengthSquared() < 200 * 200) {
                            getPathList().remove(0);
                        }
                        if (getPathList().size() > 0) {
                            newPath(getPathList().get(0));
                        }
                    }
                    setTarget(null);
                    setCommand(1);
                }
            }
        }
        super.updateData(tick);
    }

    private BaseObject searchTarget() {
        BaseObject res = null;
        for (User u : getRoom().getUsers()) {
            if (getPosition().sub(u.getPosition()).lengthSquared() < Math.pow(u.getRadius() + getRadius() + getVisibilityRadius(), 2) && getTeamId() != u.getTeamId() && u.isAlive()) {
                if (res != null) {
                    if (getPosition().sub(u.getPosition()).lengthSquared() < getPosition().sub(res.getPosition()).lengthSquared()) {
                        res = u;
                    }
                } else {
                    res = u;
                }
            }
        }
        if (res == null) {
            for (Tower t : getRoom().getTowers()) {
                if (getPosition().sub(t.getPosition()).lengthSquared() < Math.pow(t.getRadius() + getRadius() + getVisibilityRadius(), 2) && getTeamId() != t.getTeamId() && t.isAlive()) {
                    if (res != null) {
                        if (getPosition().sub(t.getPosition()).lengthSquared() < getPosition().sub(res.getPosition()).lengthSquared()) {
                            res = t;
                        }
                    } else {
                        res = t;
                    }
                }
            }
        }
        return res;
    }

    @Override
    public void doDamage(BaseObject attacker, double damage) {
        super.doDamage(attacker, damage);
    }

    @Override
    protected void calcKill() {
        super.calcKill();
        getRoom().removeUser(this);
    }

    @Override
    public void initPosition() {
        int x = 900;
        int y = 3600;
        if (getTeamId() == 2) {
            if (getLine() == 1) {
                x = 3200;
                y = 400;
            } else if (getLine() == 2) {
                x = 3600;
                y = 900;
            } else if (getLine() == 3) {
                x = 4200;
                y = 1200;
            }
        } else {
            if (getLine() == 1) {
                x = 400;
                y = 3200;
            } else if (getLine() == 2) {
                x = 900;
                y = 3600;
            } else if (getLine() == 3) {
                x = 1200;
                y = 4200;
            }
        }
        Vector2 pos = new Vector2(Util.getRandomInt(x - 200, x + 200), Util.getRandomInt(y - 200, y + 200));
        while (getUserCollision(pos.getX(), pos.getY()) != null) {
            pos = new Vector2(Util.getRandomInt(x - 200, x + 200), Util.getRandomInt(y - 200, y + 200));
        }
        this.setPosition(pos);
    }

    /**
     * @return the line
     */
    public int getLine() {
        return line;
    }

    /**
     * @param line the line to set
     */
    public void setLine(int line) {
        this.line = line;
    }

    /**
     * @return the pathList
     */
    public ArrayList<Vector2> getPathList() {
        return pathList;
    }

    /**
     * @param pathList the pathList to set
     */
    public void setPathList(ArrayList<Vector2> pathList) {
        this.pathList = pathList;
    }
}
